package com.edu.hre.struts.action.recruit;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.util.LabelValueBean;

import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.ComboBoxInit;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreCandidateLanguage;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.recruit.CanLanguageUpdateForm;

public class CanLanguageUpdateAction extends DispatchAction{
	
	private static final Logger logger = Logger.getLogger(CanLanguageUpdateAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private final String ACTION_NAME = "canLanguageUpdate";
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		CanLanguageUpdateForm beanForm = (CanLanguageUpdateForm) form;
		beanForm.setIsAddNew(true);

		if(request.getParameter(StringConstants.BTN_ADD) == null){
			
			int canId = CommonUtils.getIntParameter("can_id", request);
			beanForm.setCanId(canId);

			beanForm.setMessage(StringConstants.BLANK);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}
		
		CandidateUpdateAction.setAttributesForRequest(beanForm.getCanId(), request);
		
		HreCandidateLanguage tmp = new HreCandidateLanguage();
		tmp.setCandidateId(beanForm.getCanId());
		tmp.setLanguageId(beanForm.getLanguageId());
		tmp.setReadCompetency(beanForm.getReadCompetency());
		tmp.setSpeakCompetency(beanForm.getSpeakCompetency());
		tmp.setListenCompetency(beanForm.getListenCompetency());
		tmp.setWriteCompetency(beanForm.getWriteCompetency());
		tmp.setDateCreated(new Date());
		tmp.setDateModified(new Date());

		boolean status = baseService.saveOrUpdate(tmp);
		if (!status) {
			beanForm.setMessage(StringConstants.INSERT_FAILURE_MESSAGE);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		response.getWriter().write(StringConstants.POP_UP_CLOSE_SCRIPT);
		return null;
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		CanLanguageUpdateForm beanForm = (CanLanguageUpdateForm) form;
		beanForm.setIsAddNew(false);
		
		if(request.getParameter(StringConstants.BTN_EDIT) == null){
			
			int id = CommonUtils.getIntParameter("id", request);
			HreCandidateLanguage tmp = baseService.getFirstPojo("id", Compare.EQUAL, id, null, null, 0, HreCandidateLanguage.class);
			
			if (tmp == null) {
				beanForm.setMessage(StringConstants.BLANK);
				initFormBean(beanForm);
				return mapping.findForward(ACTION_NAME);
			}

			beanForm.setId(id);
			beanForm.setCanId((int) tmp.getCandidateId());
			beanForm.setLanguageId(tmp.getLanguageId());
			beanForm.setReadCompetency(tmp.getReadCompetency());
			beanForm.setSpeakCompetency(tmp.getSpeakCompetency());
			beanForm.setListenCompetency(tmp.getListenCompetency());
			beanForm.setWriteCompetency(tmp.getWriteCompetency());

			beanForm.setMessage(StringConstants.BLANK);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}
		
		CandidateUpdateAction.setAttributesForRequest(beanForm.getCanId(), request);

		HreCandidateLanguage tmp = baseService.getFirstPojo("id", Compare.EQUAL, beanForm.getId(), null, null, 0, HreCandidateLanguage.class);
		if (tmp == null) {
			beanForm.setMessage(StringConstants.BLANK);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		tmp.setLanguageId(beanForm.getLanguageId());
		tmp.setReadCompetency(beanForm.getReadCompetency());
		tmp.setSpeakCompetency(beanForm.getSpeakCompetency());
		tmp.setListenCompetency(beanForm.getListenCompetency());
		tmp.setWriteCompetency(beanForm.getWriteCompetency());
		tmp.setDateModified(new Date());

		boolean status = baseService.saveOrUpdate(tmp);
		if (!status) {
			beanForm.setMessage(StringConstants.UPDATE_FAILURE_MESSAGE);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		response.getWriter().write(
				 "<script language=\"javascript\">\n" +
						" parent.dw.close();" + 
						 " var param = parent.document.createElement('input'); " +
						 " param.type = 'hidden'; " +
						 " param.name = 'id'; " +
						 " param.value = " + ((int) tmp.getCandidateId()) +  ";" +
						 " parent.document.forms[0].appendChild(param);" +
						" parent.document.forms[0].submit(); \n" +
				 "</script>");
		return null;
	}
	
	private void initFormBean (CanLanguageUpdateForm beanForm) {

		String[] allComps = StringConstants.ALL_COMPETENCIES;
		List<LabelValueBean> allCompetencies = new ArrayList<LabelValueBean>();
		
		for (int ind = 0; ind <= 10; ind++) {
			allCompetencies.add(new LabelValueBean(ind + "/10 [" + allComps[ind] + "]", ind + StringConstants.BLANK));
		}

		beanForm.setAllLanguages(ComboBoxInit.getAllLanguages(false));
		beanForm.setAllCompetencies(allCompetencies);
	}
}
